#ifndef _GENO_LINK_MSG_H__
#define _GENO_LINK_MSG_H__

#include <stdint.h>

#define GOLINK_STX1         0x5A
#define GOLINK_STX2         0xA5

#define  CRC_START_16    0x0000
#define  CRC_POLY_16     0xA001

#define GOLINK_PAYLOAD_MAX_LEN      (20)      

#define FLOOR_BOARDCASET_ADDR				(0)
#define FLOOR1_ADDR							(1)
#define FLOOR2_ADDR							(2)
#define FLOOR3_ADDR							(3)
#define FLOOR4_ADDR							(4)
#define FLOOR5_ADDR							(5)
#define FLOOR6_ADDR							(6)
#define FLOOR7_ADDR							(7)
#define FLOOR8_ADDR							(8)

#pragma pack(1)
typedef struct golink_msg {
    uint8_t seq;
    uint8_t msg_len;
    uint8_t msg_id;
	uint16_t msg_crc;
    uint8_t payload[GOLINK_PAYLOAD_MAX_LEN];
}golink_msg_t;

typedef struct golink_status {
    uint8_t states;                 ///< 状态机状态
    uint8_t msg_idx;                ///< 数据接收索引

    uint8_t rx_seq;                 ///< 接收计数
    uint8_t tx_seq;                 ///< 发送计数

    uint32_t error_packet;          ///< 错误包的个数
    uint32_t start_index;           ///< 用buf 解析时候的索引
}golink_status_t;
#pragma pack()
/**
 * 底盘解析函数 返回值 枚举
 * */
typedef enum {
    GOLINK_FRAMING_INCOMPLETE = 0,             ///< 数据包不完整
    GOLINK_FRAMING_OK,                         ///< 数据正确
    GOLINK_FRAMING_BAD_CRC,                    ///< 数据包错误
    GOLINK_FRAMING_VALUE_END
}GOLINK_RECEIVE_VALUE;

/**
 * 解析数据包状态
 * */
typedef enum {
    GOLINK_STATE_UNINIT    = 0x10,
    GOLINK_STATE_IDLE,
    GOLINK_STATE_GOT_STX1,
    GOLINK_STATE_GOT_STX2,
    GOLINK_STATE_GOT_SEQ,
    GOLINK_STATE_GOT_LEN,
    GOLINK_STATE_GOT_MSGID,
    GOLINK_STATE_GOT_PAYLOAD,
    GOLINK_STATE_GOT_LSB,
    GOLINK_STATE_GOT_MSB,
    GOLINK_STATE_GOT_BAD_CRC,
    GOLINK_STATE_END
}GOLINK_STATES_VALUE;

/**
 * @brief 初始化状态
 ***/
void init_golink_status();

/**
 * @brief 发送信息
 * @param msg_id [in]   消息MSG_ID
 * @param msg_len [in]  消息长度
 * @param data [in]     消息数据
 * @return 非0 错误
 ***/
int golink_send_message(uint8_t msg_id,uint8_t msg_len,const char *data); 

/**
 * @brief 解析消息
 * @param c [in] 接收的字符
 * @param msg [out] 返回消息
 * @return GOLINK_RECEIVE_VALUE 
 ***/
int golink_parse_buf(uint8_t c,golink_msg_t *msg);

#endif //_GENO_LINK_MSG_H__
